package com.bstek.demo.urule.client;

import com.bstek.urule.Utils;
import com.bstek.urule.model.GeneralEntity;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.service.KnowledgeService;
import com.bstek.frame.model.ApiResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体育项目：案例567的测试
 */
@RestController
@RequestMapping("/rest/excel")
public class RuleDemoController {

    /**
     * excel数据检查
     * @param id 知训包id
     * @param obj 测试数据
     * @return
     * @throws IOException
     */
    @RequestMapping("urule/{id}")
    public ApiResult<?> uruleTest(@PathVariable(value = "id", required = true)  String id, @RequestBody Object obj) throws IOException {
        //创建一个KnowledgeSession对象
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge(id);
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);

        if(obj==null){
            return ApiResult.fail("上送参数不能为空！！");
        }
        if (obj instanceof List) {
            System.out.println("处理List...");
            // 处理List的逻辑
            List<Map<String,Object>> list= (List<Map<String, Object>>) obj;
            Map<String, Object> param = new HashMap<>();
            param.put("list", list);// 参数名称
            session.fireRules(param);
            System.out.println("run complete");
            Map<String, Object> outParam =session.getParameters();
            return ApiResult.success(outParam);

        } else if (obj instanceof Map) {
            System.out.println("处理Map...");
            // 处理Map的逻辑
            Map<String, Object> rowMap = (Map<String, Object>) obj;
            Map<String, Object> param = new HashMap<>();
            param.put("map", rowMap);
            session.fireRules(param);
            System.out.println("run complete");
            Map<String, Object> outParam =session.getParameters();
            return ApiResult.success(outParam);
        } else {
            System.out.println("未知类型...");
            // 处理其他类型的逻辑
            return ApiResult.fail("未知类型！！");
        }
    }

	@SuppressWarnings("unchecked")
	@RequestMapping("test1")
    public String test1() throws IOException {
        //创建一个KnowledgeSession对象
        KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("20504");
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
        
        Map<String, Object> rowMap = new HashMap<>();
        rowMap.put("A", 10);
        rowMap.put("B", 4);
        rowMap.put("C", 3);
        rowMap.put("D", 3);
        Map<String, Object> param = new HashMap<>();
        param.put("map", rowMap);
        session.fireRules(param);
        
        System.out.println("run complete");
        String result=(String) session.getParameter("result");
        System.out.println(result);
        
        Map<String, Object>  resultMap=(Map<String, Object>) session.getParameter("map");
        System.out.println(resultMap);
        return "ok";
    }
    
	/**
	 * excel集合测试
	 * @return
	 * @throws IOException
	 */
    @SuppressWarnings("unchecked")
	@RequestMapping("test2")
    public ApiResult<?> test2() throws IOException{
    	KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
        KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("21505");
        KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
        
        List<Map<String,Object>> list=new ArrayList<>();
        
        Map<String, Object> rowMap = new HashMap<>();
        rowMap.put("A", 10);
        rowMap.put("B", 4);
        rowMap.put("C", 3);
        rowMap.put("D", 3);
        list.add(rowMap);
        
        rowMap = new HashMap<>();
        rowMap.put("A", 10);
        rowMap.put("B", 3);
        rowMap.put("C", 1);
        rowMap.put("D", 2);
        list.add(rowMap);
        
        Map<String, Object> param = new HashMap<>();
        param.put("list", list);// 参数名称
        session.fireRules(param);
        System.out.println("run complete");
        
        List<Map<String,Object>>  result= (List<Map<String, Object>>) session.getParameter("list");
        System.out.println(result);
        
        return ApiResult.success(result);
    }
    
    /**
     * 体育比赛项目测试：长跑，短跑
     * @return
     * @throws IOException
     */
   	@RequestMapping("test3")
       public ApiResult<?> test3() throws IOException{
       	KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
           KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("21412");
           KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
           
           GeneralEntity stuEntity=new GeneralEntity("com.bstek.urule.test.model.SportMan");
           stuEntity.put("name", "韩大大");
           stuEntity.put("age", 35);
           stuEntity.put("sex", "男");
           //模拟长跑成绩
           List<GeneralEntity> longProjectData=new ArrayList<>();
           GeneralEntity sportEntity=new GeneralEntity("com.bstek.urule.test.model.SportData");
           sportEntity.put("project", "10000米");
           sportEntity.put("mark", 30);
           longProjectData.add(sportEntity);
           
           sportEntity=new GeneralEntity("com.bstek.urule.test.model.SportData");
           sportEntity.put("project", "20000米");
           sportEntity.put("mark", 60);
           longProjectData.add(sportEntity);
           
           sportEntity=new GeneralEntity("com.bstek.urule.test.model.SportData");
           sportEntity.put("project", "30000米");
           sportEntity.put("mark", 90);
           longProjectData.add(sportEntity);
           stuEntity.put("longProjectData", longProjectData);//长跑数据集合
           
           //模拟短跑成绩
           List<GeneralEntity> shortProjectData=new ArrayList<>();
           sportEntity=new GeneralEntity("com.bstek.urule.test.model.SportData");
           sportEntity.put("project", "100米");
           sportEntity.put("mark", 1);
           shortProjectData.add(sportEntity);
           
           sportEntity=new GeneralEntity("com.bstek.urule.test.model.SportData");
           sportEntity.put("project", "200米");
           sportEntity.put("mark", 3);
           shortProjectData.add(sportEntity);
           
           sportEntity=new GeneralEntity("com.bstek.urule.test.model.SportData");
           
           sportEntity.put("project", "400米");
           sportEntity.put("mark", 9);
           shortProjectData.add(sportEntity);
           stuEntity.put("shortProjectData", shortProjectData);//短跑数据集合
           
           //根据变量库设置的默认值设置变量实例
           Utils.assignVariableObjectDefaultValue(stuEntity,session);
           
           session.insert(stuEntity);
           session.fireRules();
           
           System.out.println("run complete");
           return ApiResult.success(stuEntity);
    }
    /**
     * 体育比赛项目测试：长跑，短跑
     * @return
     * @throws IOException

   	@RequestMapping("test4")
       public ApiResult<?> test4() throws IOException{
           KnowledgeService knowledgeService = (KnowledgeService) Utils.getApplicationContext().getBean(KnowledgeService.BEAN_ID);
           KnowledgePackage knowledgePackage = knowledgeService.getKnowledge("21412");
           KnowledgeSession session = KnowledgeSessionFactory.newKnowledgeSession(knowledgePackage);
           
           SportMan sportMan=new SportMan("韩大大","男",35);
           
           //模拟长跑成绩
           List<SportData> longProjectData=new ArrayList<>();
           SportData sportData=new SportData("10000米",30);
           longProjectData.add(sportData);
           sportData=new SportData("20000米",60);
           longProjectData.add(sportData);
           sportData=new SportData("30000米",90);
           longProjectData.add(sportData);
           sportMan.setLongProjectData(longProjectData);//长跑数据集合
           
           //模拟短跑成绩
           List<SportData> shortProjectData=new ArrayList<>();
           sportData=new SportData("100米",1);
           shortProjectData.add(sportData);
           sportData=new SportData("200米",3);
           shortProjectData.add(sportData);
           sportData=new SportData("400米",9);
           shortProjectData.add(sportData);
           sportMan.setShortProjectData(shortProjectData);//短跑数据集合
          
           session.insert(sportMan);
           RuleExecutionResponse res= session.fireRules();
           
           System.out.println("run complete:"+ JsonUtils.toJSON(res));
           return ApiResult.success(sportMan);
       }
     */

    @RequestMapping("/tt/{id}/{ver}")
    public ApiResult<?> tt(
                           @PathVariable(value = "id")  String id,
                           @PathVariable(value = "ver", required =false)  String ver,
                           @RequestBody Object obj
                           ){
        // @PathVariable(value = "ver", required =false)  String ver 不会生效的，按路径匹配报错！！
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("ver",ver);
        return ApiResult.success(map);
    }

    @RequestMapping("/tt/{id}")
    public ApiResult<?> tt2(
            @PathVariable(value = "id")  String id,
            @RequestBody Object obj ){
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        return ApiResult.success(map);
    }

    @RequestMapping(value = {"/aa/{id}/{ver}","/aa/{id}"})
    public ApiResult<?> aa(
            @PathVariable(value = "id")  String id,
            @PathVariable(value = "ver", required =false)  String ver,
            @RequestBody Object obj ){
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        map.put("ver",ver);
        return ApiResult.success(map);
    }
}